package crowMessenger;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;
import javax.swing.table.DefaultTableModel;



public class AccountManagerFrame extends JDialog{
	private static final long serialVersionUID = -525969209001580966L;
	private JTable table = new JTable();
	protected MainPanel con;

	public AccountManagerFrame(MainPanel con){
		this.con = con;
		setTitle("Accounts");
		setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		setLocation(550, 100);
		getContentPane().add(accManagerPanel());
		setModal(true);
		setResizable(false);
		pack();
		setVisible(true);
	}

	/* Main panel of this frame */
	private JPanel accManagerPanel(){
		JPanel panel = new JPanel(new BorderLayout());
		panel.setPreferredSize(new Dimension(600,400));
		panel.add(accScrollPane(),BorderLayout.CENTER);
		panel.add(rightPanel(),BorderLayout.EAST);
		panel.add(leftPanel(),BorderLayout.WEST);
		panel.add(btnPanel(),BorderLayout.SOUTH);
		return panel;
	}

	/* the scrollpane which contains the table */
	private JScrollPane accScrollPane(){
		accTable();
		JScrollPane scrollPane = new JScrollPane(table);
		scrollPane.setPreferredSize(new Dimension(400,275));
		return scrollPane;
	}

	/* fill the table, would be revoked after(add,edit,remove),the Actual table of users with 3 columns */
	private void accTable(){
		Object[][] data = new Object[Accounts.size()][Accounts.size()*3];
		for(int i = 0; i < Accounts.size(); i++){
			data[i][0] = Accounts.getAccountList().get(i).isActivated();
			data[i][1] = Accounts.getAccountList().get(i).getUser();
			data[i][2] = Accounts.getAccountList().get(i).getStatus();
		}
		Object[] col = {"Activate","Account","Status"};
		table.setModel(new DefaultTableModel(data, col){
			private static final long serialVersionUID = -4878186849511927711L;

			@Override
			public Class<?> getColumnClass(int columnIndex) {
				if (getColumnName(columnIndex).equals("Activate")) {
					return Boolean.class;
				}
				return super.getColumnClass(columnIndex);
			}

			@Override
			public boolean isCellEditable(int row, int column){  
				if(column == 0)
					return true;
				return false;  
			}
		});
		table.setBackground(Color.gray);
		table.setFocusable(false);
		table.setRowHeight(40);
		table.getColumnModel().getColumn(0).setMaxWidth(60);
		table.getColumnModel().getColumn(0).setMinWidth(60);
		table.getColumnModel().getColumn(2).setMaxWidth(90);
		table.getColumnModel().getColumn(2).setMinWidth(90);
		table.addMouseListener(new MouseAdapter() {
			public void mouseClicked(MouseEvent e) {
				JTable target = (JTable)e.getSource();
				int row = target.getSelectedRow();
				int column = target.getSelectedColumn();
				if(column == 0){
					if((boolean) table.getValueAt(row, column)){
						Accounts.accList.get(Accounts.getIndex((String)table.getValueAt(row, 1))).activate();
						accTable();
					}
					else{
						Accounts.accList.get(Accounts.getIndex((String)table.getValueAt(row, 1))).deactivate();
						accTable();
					}
				}
			}
		});
	}

	/* dummy panel to fill out extra space might needed for fancy gui design */
	private JPanel rightPanel(){
		JPanel panel = new JPanel();
		panel.setPreferredSize(new Dimension(100,300));
		return panel;
	}

	/* dummy panel to fill out extra space might needed for fancy gui design */
	private JPanel leftPanel(){
		JPanel panel = new JPanel();
		panel.setPreferredSize(new Dimension(100,300));
		return panel;
	}

	/* contains the panels of buttons */
	private JPanel btnPanel(){
		JPanel panel = new JPanel(new GridLayout(1, 3));
		panel.setPreferredSize(new Dimension(600,125));
		panel.add(addPanel());
		panel.add(editPanel());
		panel.add(removePanel());
		return panel;
	}

	/* contains the add button */
	private JPanel addPanel(){
		JPanel panel = new JPanel(new GridBagLayout());
		JButton btn = new JButton("Add");
		btn.setPreferredSize(new Dimension(100, 30));
		btn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				new AddAccountFrame(con);
				accTable();
			}
		});
		panel.add(btn);
		return panel;
	}

	/* contains the edit button */
	private JPanel editPanel(){
		JPanel panel = new JPanel(new GridBagLayout());
		JButton btn = new JButton("Edit");
		btn.setPreferredSize(new Dimension(100, 30));
		btn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				if(table.getSelectedRowCount() == 1){
					new EditAccountFrame(Accounts.getIndex((String)table.getValueAt(table.getSelectedRow(), 1)));
					accTable();
				}
			}
		});
		panel.add(btn);
		return panel;
	}

	/* contains the remove button */
	private JPanel removePanel(){
		JPanel panel = new JPanel(new GridBagLayout());
		JButton btn = new JButton("Remove");
		btn.setPreferredSize(new Dimension(100, 30));
		btn.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				int[] selectedRows = new int[table.getSelectedRowCount()];
				selectedRows = table.getSelectedRows();
				for(int i = 0; i < selectedRows.length; i++)
					Accounts.remove(Accounts.search((String)table.getValueAt(selectedRows[i], 1)));
				accTable();
			}
		});
		panel.add(btn);
		return panel;
	}
}
